<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<body>

<script>

// functions to detect if we are running inside an iOS Webview 
function isiOS(){
    return window.webkit;
}

function displayiOS(){
    if (isiOS()){
      document.querySelector('#placeholder').innerHTML = 'running iOS Webview';
    } else {
      document.querySelector('#placeholder').innerHTML = 'not running iOS Webview';
  }
}

// function to send a message to ios switft code to start to record audio
function start_recording() {
    document.getElementById('placeholder').innerHTML = 'start_recording called';
    window.webkit.messageHandlers["scriptHandler"].postMessage("startCapture");
}

// function to send a message to ios switft code to stop recording audio
function stop_recording() {
    document.getElementById('placeholder').innerHTML = 'stop_recording called';
    window.webkit.messageHandlers["scriptHandler"].postMessage("stopCapture");
}

// function to send a message to ios switft code to play latest recording
function play_recording() {
    document.getElementById('placeholder').innerHTML = 'play_recording called';
    window.webkit.messageHandlers["scriptHandler"].postMessage("playCapture");
}

// function to send a message to ios switft code to stop playing audio
function stop_playing() {
    document.getElementById('placeholder').innerHTML = 'stop_playing called';
    window.webkit.messageHandlers["scriptHandler"].postMessage("stopPlaying");
}

// function to open settings app
function open_settings() {
    window.vcopensettings();
}

// this is called by swift when the recording stops
function uploadData(sBinaryParam) {
    // This 'atob' decodes the Base64 encoded data sent by swift
    let sDecodedParam = window.atob(sBinaryParam);
    // this is the blob we should upload blob to the server
    let blob = new Blob([sDecodedParam])
    // let's check the size of the blob to see if we have actual data
    document.getElementById('placeholder').innerHTML = "Blob size" + blob.size;
}

// this is called by swift every 0.1 seconds with the peak and current dblevel 
// of the audio being captured
function levels(dblevel, peaklevel){
    document.querySelector('#placeholder').innerHTML = 'peaklevel: ' + peaklevel + ' - dblevel: ' + dblevel;
}

function nativemsgs(permission){
    document.querySelector('#placeholder').innerHTML = 'Native msgs ' + permission;
}

function orientationapi(orientation){
    window.webkit.messageHandlers["scriptHandler"].postMessage(orientation);
}

</script>

<br><br><br>
<input type="button" value="Start Recording" width="300" height="300" onclick="start_recording()"/> <br> <br>
<input type="button" value="Stop Recording" width="300" height="300" onclick="stop_recording()"/>  <br> <br>
<input type="button" value="Play Recording" width="300" height="300" onclick="play_recording()"/>  <br> <br>
<input type="button" value="Stop Recording" width="300" height="300" onclick="stop_playing()"/>  <br> <br>
<input type="button" value="Open Settings" width="300" height="300" onclick="open_settings()"/>  <br> <br>
<input type="button" value="Lock portrait orientation" width="300" height="300" onclick="orientationapi('lockportrait')"/>  <br> <br>
<input type="button" value="Lock landscape orientation" width="300" height="300" onclick="orientationapi('locklandscape')"/>  <br> <br>
<input type="button" value="Unlock orientation" width="300" height="300" onclick="orientationapi('unlockall')"/>  <br> <br>

<div id='placeholder'></div>
<script> displayiOS(); </script>
</body>
</html>
